package cn.stylefeng.guns.sys.modular.tao.service.serviceImpl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.tao.entity.Depository;
import cn.stylefeng.guns.sys.modular.tao.mapper.DepositoryMapper;
import cn.stylefeng.guns.sys.modular.tao.model.params.DepositoryParam;
import cn.stylefeng.guns.sys.modular.tao.model.result.DepositoryResult;
import  cn.stylefeng.guns.sys.modular.tao.service.DepositoryService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 仓库表 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-09-11
 */
@Service
public class DepositoryServiceImpl extends ServiceImpl<DepositoryMapper, Depository> implements DepositoryService {

    @Autowired
    DepositoryMapper depositoryMapper;
    @Override
    public void add(DepositoryParam param){
        Depository entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DepositoryParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(DepositoryParam param){
        Depository oldEntity = getOldEntity(param);
        Depository newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DepositoryResult findBySpec(DepositoryParam param){
        return null;
    }

    @Override
    public List<DepositoryResult> findListBySpec(DepositoryParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DepositoryParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public DepositoryParam getByKtypeId(String ktypeid) {
        return depositoryMapper.getByKtypeId(ktypeid);
    }

    @Override
    public Integer updateByKtypeId(DepositoryParam depository) {
        return depositoryMapper.updateByKtypeId(depository);
    }

    @Override
    public List<String> getAllDepository() {
        return depositoryMapper.getAllDepository();
    }

    @Override
    public List<Depository> getAllDepositorys() {
        return depositoryMapper.getAllDepositorys();
    }

    private Serializable getKey(DepositoryParam param){
        return param.getKtypeId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Depository getOldEntity(DepositoryParam param) {
        return this.getById(getKey(param));
    }

    private Depository getEntity(DepositoryParam param) {
        Depository entity = new Depository();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
